package com.smartsofa.fragment;

import java.io.File;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.smartsofa.R;
import com.smartsofa.main.ChangePassword;
import com.smartsofa.main.LoginActivity;
import com.smartsofa.main.UserInfoActivity;
import com.smartsofa.util.MyUtils;

public class MenuListFragment extends Fragment {
	private ListView list;
	private ImageView user_image;
	private TextView username;
	private Intent intent = new Intent();
	private SharedPreferences sp;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		return inflater.inflate(R.layout.menu_list, null);
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		list = (ListView) getActivity().findViewById(R.id.menulist);
		SampleAdapter adapter = new SampleAdapter(getActivity());
		user_image = (ImageView) getActivity().findViewById(R.id.user_image);

		String baseDir = Environment.getExternalStorageDirectory()
				.getAbsolutePath();
		String fileName = "/user_img_" + MyUtils.getUSERID() + ".png";
		Bitmap bitmap = BitmapFactory.decodeFile(baseDir + fileName);
		if (bitmap != null) {
			Log.i("提取本地图片", "success");
			Drawable drawable = new BitmapDrawable(bitmap);
			user_image.setImageDrawable(drawable);
		} else {

		}

		username = (TextView) getActivity().findViewById(R.id.user_name);
		SharedPreferences sharedPreferences = getActivity()
				.getSharedPreferences("userInformation", 0);
		String string = sharedPreferences.getString("username", "用户名");
		username.setText(string);

		adapter.add(new SampleItem("用户资料", R.drawable.list_userinfo));
		adapter.add(new SampleItem("修改密码", R.drawable.list_changepw));
		adapter.add(new SampleItem("设        置", R.drawable.list_logout));
		adapter.add(new SampleItem("注        销", R.drawable.list_setting));

		list.setAdapter(adapter);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (position) {
				case 0:
					intent = new Intent(getActivity(), UserInfoActivity.class);
					startActivity(intent);
					break;
				case 1:
					intent.setClass(getActivity(), ChangePassword.class);
					startActivity(intent);
					break;
				case 2:
					Toast.makeText(getActivity(), "设置", Toast.LENGTH_SHORT)
							.show();
					break;
				case 3:
					AlertDialog.Builder builder = new AlertDialog.Builder(
							getActivity());
					builder.setMessage("确定注销？")
							.setCancelable(false)
							.setPositiveButton("确定",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											sp = getActivity()
													.getSharedPreferences(
															"userInformation",
															0);
											sp.edit().clear().commit();
											String imagefileString = Environment
													.getExternalStorageDirectory()
													+ "/user_img_"
													+ MyUtils.getUSERID()
													+ ".png";
											File file = new File(
													imagefileString);
											if(file.exists()){
												file.delete();
											}
											
											Log.i("删除图片", "success");
											Toast.makeText(getActivity(),
													"注销成功！", Toast.LENGTH_SHORT)
													.show();

											intent.setClass(getActivity(),
													LoginActivity.class);
											startActivity(intent);
											dialog.cancel();
											getActivity().finish();

										}
									})
							.setNegativeButton("取消",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											dialog.cancel();
										}
									});
					AlertDialog alert = builder.create();
					alert.show();
					break;

				}
			}
		});
	}

	public class SampleAdapter extends ArrayAdapter<SampleItem> {
		public SampleAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.menu_list_row, null);
			}
			ImageView icon = (ImageView) convertView
					.findViewById(R.id.row_icon);
			icon.setImageResource(getItem(position).iconRes);
			TextView title = (TextView) convertView
					.findViewById(R.id.row_title);
			title.setText(getItem(position).tag);

			return convertView;
		}
	}

	private class SampleItem {
		public String tag;
		public int iconRes;

		public SampleItem(String tag, int iconRes) {
			this.tag = tag;
			this.iconRes = iconRes;
		}
	}

	@Override
	public void onResume() {

		String baseDir = Environment.getExternalStorageDirectory()
				.getAbsolutePath();
		String fileName = "/user_img_"+MyUtils.getUSERID()+".png";
		Bitmap bitmap = BitmapFactory.decodeFile(baseDir + fileName);
		if (bitmap != null) {
			Log.i("提取本地图片", "success");
			Drawable drawable = new BitmapDrawable(bitmap);
			user_image.setImageDrawable(drawable);
			SharedPreferences useinfo = getActivity().getSharedPreferences(
					"userInformation", 0);
			String username = useinfo.getString("username", "用户名");
			this.username.setText(username);
		}
		super.onResume();

	}
}
